import {
  Component,
  OnInit,
  Input,
  Output,
  EventEmitter,
  OnDestroy,
} from '@angular/core';
import { UserService } from 'src/app/service/user.service';
import { BaseService } from 'src/app/service/base.service';
import { UtilsService } from 'src/app/service/utils.service';
import { YearawardService } from '../yearaward.service';
@Component({
  selector: 'app-activitying',
  templateUrl: './activitying.component.html',
  styleUrls: ['./activitying.component.scss', './../commonList.scss'],
})
export class ActivityingComponent implements OnInit, OnDestroy {
  @Input() timeConfig: any[] = []; // 时间配置
  @Output() goToActivity = new EventEmitter<void>();
  public deployUrl = '';
  public showPage = '';
  public progressWidth: any = 0;
  public timeArr: any = [];
  public nowDate: any;
  public activityRank: number = 0;
  public navArr = [
    { name: '海选赛', tips: 'N进100', activityRank: 1, stageName: 'haixuan' },
    { name: '晋级赛', tips: '100进64', activityRank: 2, stageName: 'jinji' },
    { name: '冲刺赛', tips: '64进48', activityRank: 3, stageName: 'chongci' },
    {
      name: '直通赛',
      tips: '前6直通决赛',
      activityRank: 4,
      stageName: 'zhitong',
    },
    { name: '半决赛', tips: '26进10', activityRank: 5, stageName: 'banjue' },
    { name: '决赛', tips: '16进10', activityRank: 6, stageName: 'juesai' },
  ];
  constructor(
    private utilsservice: UtilsService,
    private baseservice: BaseService,
    private userService: UserService,
    private yearawardService: YearawardService
  ) {}
  public activityStart = ''; // 判断鹊桥榜是否显示
  public reshing: any = null;
  ngOnInit(): void {
    this.activityStart = this.yearawardService.getStartType();
    this.deployUrl = this.userService.getDeployUrl();
    this.initPage(1);
    this.reshing = this.utilsservice.MsgSub.subscribe((res: any) => {
      this.activityStart = this.yearawardService.getStartType();
      this.initPage(null);
    });
  }
  public initPage(val) {
    const nvshenArray = [31, 17, 36, 7, 34, 18, 19, 35];
    this.timeArr = this.timeConfig
      .filter((itm) => nvshenArray.includes(itm.type))
      .sort(this.sortBy('startTime', undefined)); // 时间配置
    this.checkActivityType(val);
  }
  ngOnDestroy() {
    if (this.reshing) {
      this.reshing.unsubscribe();
    }
  }
  /**
   * 计算当前活动阶段
   */
  public checkActivityType(val) {
    this.nowDate = new Date().getTime();
    if (this.nowDate > this.timeArr[this.timeArr.length - 1].endTime * 1000) {
      this.activityRank = 6;
      if (val) {
        this.showPage = 'juesai';
      }
      this.progressWidth = '100%';
      return;
    }
    for (let i = 0; i < this.timeArr.length; i++) {
      if (
        this.nowDate >= this.timeArr[i].startTime * 1000 &&
        this.nowDate <= this.timeArr[i].endTime * 1000
      ) {
        const nowGame = this.timeArr[i]; // 现在的比赛阶段
        /**
         *
         * type=31(女神海选) 17(女神晋级) 36(冲刺赛) 7(直通赛入围) 34(直通赛吃鸡) 18(半决赛) 19(决赛) 35(5强对决)
         */
        if (nowGame.type === 31) {
          // 海选
          if (val) {
            this.showPage = 'haixuan';
          }
          this.activityRank = 1;
          this.progressWidth = '8%';
        }
        if (nowGame.type === 17) {
          // 晋级
          this.activityRank = 2;
          if (val) {
            this.showPage = 'jinji';
          }
          this.progressWidth = '24%';
        }
        if (nowGame.type === 36) {
          // 冲刺
          this.activityRank = 3;
          if (val) {
            this.showPage = 'chongci';
          }
          this.progressWidth = '42%';
        }
        if (nowGame.type === 7 || nowGame.type === 34) {
          // 直通
          this.activityRank = 4;
          if (val) {
            this.showPage = 'zhitong';
          }
          this.progressWidth = '58%';
        }
        if (nowGame.type === 18) {
          // 半决
          this.activityRank = 5;
          if (val) {
            this.showPage = 'banjue';
          }
          this.progressWidth = '74%';
        }
        if (nowGame.type === 19 || nowGame.type === 35) {
          // 决赛
          this.activityRank = 6;
          if (val) {
            this.showPage = 'juesai';
          }
          this.progressWidth = '100%';
        }
        break;
      } else if (
        this.nowDate > this.timeArr[i].endTime * 1000 &&
        this.nowDate < this.timeArr[i + 1].startTime * 1000
      ) {
        const nowGame = this.timeArr[i]; // 现在的比赛阶段

        /**
         *
         * type=31(女神海选) 17(女神晋级) 36(冲刺赛) 7(直通赛入围) 34(直通赛吃鸡) 18(半决赛) 19(决赛) 35(5强对决)
         */
        if (nowGame.type === 31) {
          // 海选
          if (val) {
            this.showPage = 'haixuan';
          }
          this.activityRank = 1;
          this.progressWidth = '8%';
        }
        if (nowGame.type === 17) {
          // 晋级
          this.activityRank = 2;
          if (val) {
            this.showPage = 'jinji';
          }
          this.progressWidth = '24%';
        }
        if (nowGame.type === 36) {
          // 冲刺
          this.activityRank = 3;
          if (val) {
            this.showPage = 'chongci';
          }
          this.progressWidth = '42%';
        }
        if (nowGame.type === 7 || nowGame.type === 34) {
          // 直通
          this.activityRank = 4;
          if (val) {
            this.showPage = 'zhitong';
          }
          this.progressWidth = '58%';
        }
        if (nowGame.type === 18) {
          // 半决
          this.activityRank = 5;
          if (val) {
            this.showPage = 'banjue';
          }
          this.progressWidth = '74%';
        }
        if (nowGame.type === 19 || nowGame.type === 35) {
          // 决赛
          this.activityRank = 6;
          if (val) {
            this.showPage = 'juesai';
          }
          this.progressWidth = '100%';
        }
      }
    }
    if (this.showPage === '') {
      if (this.nowDate < this.timeArr[0].startTime * 1000) {
        this.showPage = 'haixuan';
        this.activityRank = 1;
        this.progressWidth = '8%';
      }
    }
  }
  // 排序方法 attr为排序key,rev false升序 true降序
  public sortBy(attr: any, rev: any) {
    if (rev === undefined) {
      rev = 1;
    } else {
      rev = rev ? 1 : -1;
    }

    return (a, b) => {
      a = a[attr];
      b = b[attr];
      if (a < b) {
        return rev * -1;
      }
      if (a > b) {
        return rev * 1;
      }
      return 0;
    };
  }
  public selectNav(name: string) {
    const nowTime = new Date().getTime();
    if (name === 'jinji') {
      // 晋级
      const nowActivity = this.timeArr.filter((itm) => itm.type === 17); // 时间配置
      if (nowTime < nowActivity[0].startTime * 1000) {
        this.baseservice.showToast('活动暂未开始', 1500);
        return;
      }
    }
    if (name === 'chongci') {
      // 冲刺
      const nowActivity = this.timeArr.filter((itm) => itm.type === 36); // 时间配置
      if (nowTime < nowActivity[0].startTime * 1000) {
        this.baseservice.showToast('活动暂未开始', 1500);
        return;
      }
    }
    if (name === 'zhitong') {
      // 直通
      const nowActivity = this.timeArr.filter(
        (itm) => itm.type === 7 || itm.type === 34
      ); // 时间配置
      if (nowTime < nowActivity[0].startTime * 1000) {
        this.baseservice.showToast('活动暂未开始', 1500);
        return;
      }
    }
    if (name === 'banjue') {
      // 半决
      const nowActivity = this.timeArr.filter((itm) => itm.type === 18); // 时间配置
      if (nowTime < nowActivity[0].startTime * 1000) {
        this.baseservice.showToast('活动暂未开始', 1500);
        return;
      }
    }
    if (name === 'juesai') {
      // 决赛
      const nowActivity = this.timeArr.filter(
        (itm) => itm.type === 19 || itm.type === 35
      ); // 时间配置
      if (nowTime < nowActivity[0].startTime * 1000) {
        this.baseservice.showToast('活动暂未开始', 1500);
        return;
      }
    }

    this.showPage = name;
  }
  public buffToActivity() {
    this.goToActivity.emit();
  }
  onTabClick(tab) {}
}
